package com.chen.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chen.dto.SetmealDto;
import com.chen.entity.R;
import com.chen.entity.Setmeal;
import com.chen.entity.SetmealDish;
import com.chen.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    @GetMapping("/page")
    public R page(int page, int pageSize, String name) {
        Page<SetmealDto> setmealpage = setmealService.setmealpage(page, pageSize, name);
        return R.success(setmealpage);
    }


    @PostMapping
    @CacheEvict(value = "setmeal",allEntries = true)
    public R add(@RequestBody SetmealDto setmealDto) {
        setmealService.add(setmealDto);
        return R.success(null);
    }

    @GetMapping("{id}")
    public R findbyid(@PathVariable long id) {
        SetmealDto setmealDto = setmealService.findOne(id);
        return R.success(setmealDto);
    }


    @PutMapping
    public R update(@RequestBody SetmealDto setmealDto) {
        setmealService.upDate(setmealDto);
        return R.success(null);
    }

    @PostMapping("/status/{status}")
    public R status(long[] ids, @PathVariable int status) {
        setmealService.status(ids, status);
        return R.success(null);
    }

    @DeleteMapping
    @CacheEvict(value = "setmeal",allEntries = true)
    public R delete(@RequestParam List<Long> ids) {
        setmealService.delt(ids);
        return R.success(null);
    }

    @Cacheable(value = "setmeal", key ="'categoryId:'+#categoryId+'status:'+#status")
    @GetMapping("/list")
    public R list(Long categoryId, Integer status) {
        List<Setmeal> list = setmealService.findByid(categoryId, status);
        return R.success(list);
    }

    @GetMapping("/dish/{setmealId}")
    public R get(@PathVariable Long setmealId) {
        List<SetmealDish> list = setmealService.get(setmealId);
        return R.success(list);
    }
}